import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

export const constantRoutes = [{
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },
  {
    path: "/oauth/github/callback",
    component: () => import("@/views/oauth/github"),
    hidden: true
  },
  {
    path: "/oauth/qq/callback",
    component: () => import("@/views/oauth/qq"),
    hidden: true
  },
  {
    path: "/oauth/gitee/callback",
    component: () => import("@/views/oauth/gitee"),
    hidden: true
  },
  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [{
      path: "dashboard",
      name: "Dashboard",
      component: () => import("@/views/dashboard/index"),
      meta: {
        title: "Dashboard",
        icon: "dashboard",
        affix: true
      }
    }]
  },
  {
    path: "/personal",
    component: Layout,
    redirect: "/personal/index",
    hidden: true,
    children: [{
      path: "index",
      name: "personal_index",
      component: () => import("@/views/info/index"),
      hidden: true,
      meta: {
        title: "Personal Information",
        icon: "dashboard"
      }
    }]
  }
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  {
    path: "/salemodel",
    component: Layout,
    meta: {
      title: "Sale Model",
      icon: "category",
      roles: ["saleorders.menu", "saleinvoices.menu", "quotationlists.menu"]
    },
    redirect: "/salemodel",
    alwaysShow: true,
    children: [{
        path: "quotationlist",
        name: "quotationlist_index",
        component: () => import("@/views/salemodel/quotationlist/index"),
        meta: {
          title: "Quotation list",
          roles: ["quotationlists.menu"],
          icon: "list"
        }
      },
      {
        path: "saleorder",
        name: "saleorder_index",
        component: () => import("@/views/salemodel/saleorder/index"),
        meta: {
          title: "Sale Order",
          roles: ["saleorders.menu"],
          icon: "list"
        }
      },
      {
        path: "saleinvoice",
        name: "saleinvoice_index",
        component: () => import("@/views/salemodel/saleinvoice/index"),
        meta: {
          title: "Sale Invoice",
          roles: ["saleinvoices.menu"],
          icon: "list"
        }
      }
    ]
  },
  {
    path: "/customer",
    component: Layout,
    meta: {
      title: "Customer",
      icon: "category",
      roles: ["customers.menu", "customer_groups.menu"]
    },
    redirect: "/customer/category",
    alwaysShow: true,
    children: [{
        path: "customer",
        name: "customer_index",
        component: () => import("@/views/customer/customer/index"),
        meta: {
          title: "Customer",
          roles: ["customers.menu"],
          icon: "list"
        }
      },
      {
        path: "customer_group",
        name: "customer_group_index",
        component: () => import("@/views/customer/customer_group/index"),
        meta: {
          title: "Customer Group",
          roles: ["customer_groups.menu"],
          icon: "list"
        }
      }
    ]
  },
  {
    path: "/supplier",
    component: Layout,
    meta: {
      title: "Supplier",
      icon: "category",
      roles: ["suppliers.menu"]
    },
    redirect: "/supplier/list",
    alwaysShow: true,
    children: [{
      path: "supplierlist",
      name: "supplierlist_index",
      component: () => import("@/views/supplier/supplier/index"),
      meta: {
        title: "Supplier list",
        roles: ["suppliers.menu"],
        icon: "list"
      }
    }]
  },
  {
    path: "/product",
    component: Layout,
    meta: {
      title: "Product",
      icon: "list",
      roles: ["products.menu", "product_categorys.menu"]
    },
    redirect: "/product/category",
    alwaysShow: true,
    children: [{
        path: "productlist",
        name: "productlist_index",
        component: () => import("@/views/product/product/index"),
        meta: {
          title: "Product list",
          roles: ["products.menu"],
          icon: "list"
        }
      },
      {
        path: "category",
        name: "category_index",
        component: () => import("@/views/product/product_category/index"),
        meta: {
          title: "Product Category",
          roles: ["product_categorys.menu"],
          icon: "list"
        }
      }
    ]
  },
  {
    path: "/Inventory",
    component: Layout,
    meta: {
      title: "Inventory",
      icon: "category",
      roles: ["stocksins.menu", "transferorders.menu", "uomorders.menu", "stocktakes.menu", "warehouses.menu"]
    },
    redirect: "/Inventory",
    alwaysShow: true,
    children: [{
        path: "stocksin",
        name: "stocksin_index",
        component: () => import("@/views/inventory/stocksin/index"),
        meta: {
          title: "Stock In",
          roles: ["stocksins.menu"],
          icon: "list"
        }
      },
      {
        path: "transferorder",
        name: "transferorder_index",
        component: () => import("@/views/inventory/transferorder/index"),
        meta: {
          title: "Transfer Order",
          roles: ["transferorders.menu"],
          icon: "list"
        }
      },
      {
        path: "uomorder",
        name: "uomorder_index",
        component: () => import("@/views/inventory/uomorder/index"),
        meta: {
          title: "UOM Order",
          roles: ["uomorders.menu"],
          icon: "list"
        }
      },
      {
        path: "stocktake",
        name: "stocktake_index",
        component: () => import("@/views/inventory/stocktake/index"),
        meta: {
          title: "Stock take",
          roles: ["stocktakes.menu"],
          icon: "list"
        }
      },
      {
        path: "warehouse",
        name: "warehouse_index",
        component: () => import("@/views/inventory/warehouse/index"),
        meta: {
          title: "Warehouse",
          roles: ["warehouses.menu"],
          icon: "list"
        }
      }
    ]
  },
  {
    path: "/puchase",
    component: Layout,
    meta: {
      title: "Purchase",
      icon: "category",
      roles: ["purchaseorders.menu", "purchasereturnlists.menu"]
    },
    redirect: "/puchase",
    alwaysShow: true,
    children: [{
        path: "purchaseorder",
        name: "purchaseorder_index",
        component: () => import("@/views/puchase/purchaseorder/index"),
        meta: {
          title: "Purchase Order",
          roles: ["purchaseorders.menu"],
          icon: "list"
        }
      },
      {
        path: "purchasereturnlist",
        name: "purchasereturnlist_index",
        component: () => import("@/views/puchase/purchasereturnlist/index"),
        meta: {
          title: "Purchase Return",
          roles: ["purchasereturnlists.menu"],
          icon: "list"
        }
      }
    ]
  },
  {
    path: "/quotationtemplatelist",
    component: Layout,
    meta: {
      title: "Quotation Template",
      icon: "category",
      roles: ["quotationtemplates.menu"]
    },
    redirect: "/quotationtemplatelist",
    alwaysShow: true,
    children: [{
      path: "quotationtemplatelist",
      name: "quotationtemplatelist_index",
      component: () => import("@/views/quotationtemplate/quotationtemplatelist/index"),
      meta: {
        title: "Quotation Template",
        roles: ["quotationtemplates.menu"],
        icon: "list"
      }
    }, ]
  },
  {
    path: "/report",
    component: Layout,
    meta: {
      title: "Report",
      icon: "category",
      roles: ["salereport.menu", "inventoryreport.menu", "customerpayment.menu", "customerpaybackreport.menu"]
    },
    redirect: "/report",
    alwaysShow: true,
    children: [{
        path: "salereport",
        name: "salereport_index",
        component: () => import("@/views/report/salereport/index"),
        meta: {
          title: "Sale report",
          roles: ["salereport.menu"],
          icon: "list"
        }
      },
      {
        path: "inventoryreport",
        name: "inventoryreport_index",
        component: () => import("@/views/report/inventoryreport/index"),
        meta: {
          title: "Inventory report",
          roles: ["inventoryreport.menu"],
          icon: "list"
        }
      }, {
        path: "customerpayment",
        name: "customerpayment_index",
        component: () => import("@/views/report/customerpayment/index"),
        meta: {
          title: "Other report",
          roles: ["customerpayment.menu"],
          icon: "list"
        }
      }, {
        path: "customerpaybackreport",
        name: "customerpaybackreport_index",
        component: () => import("@/views/report/customerpaybackreport/index"),
        meta: {
          title: "Customer Payment",
          roles: ["customerpaybackreport.menu"],
          icon: "list"
        }
      }
    ]
  },
  {
    path: "/admin",
    component: Layout,
    meta: {
      title: "Set",
      icon: "category",
      roles: ["admin.menu", "roles.menu", "modules.menu", "shipmenterms.menu", "brands.menu",
        "companyattentions.menu"
      ]
    },
    redirect: "/admin/index",
    alwaysShow: true,
    children: [{
        path: "index",
        name: "admin_index",
        component: () => import("@/views/system/admin/index"),
        meta: {
          title: "Account",
          roles: ["admin.menu"],
          icon: "user"
        }
      },
      {
        path: "Role",
        name: "role_index",
        component: () => import("@/views/system/role/index"),
        meta: {
          title: "Role",
          roles: ["roles.menu"],
          icon: "form"
        }
      },
      {
        path: "brand",
        name: "brand_index",
        component: () => import("@/views/system/brand/index"),
        meta: {
          title: "Brand",
          roles: ["brands.menu"],
          icon: "form"
        }
      }, {
        path: "companyattention",
        name: "companyattention_index",
        component: () => import("@/views/system/companyattent/index"),
        meta: {
          title: "VT Attention",
          roles: ["companyattentions.menu"],
          icon: "form"
        }
      },
      {
        path: "shipmenttermlist",
        name: "shipmenttermlist_index",
        component: () => import("@/views/system/shipmenttermlist/index"),
        meta: {
          title: "Shipment Term",
          roles: ["shipmenterms.menu"],
          icon: "form"
        }
      },
      {
        path: "permission",
        name: "permission_index",
        component: () => import("@/views/system/module/index"),
        meta: {
          title: "Permission",
          roles: ["modules.menu"],
          icon: "lock"
        }
      },
    ]
  },
  {
    path: '/stocksin',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "stocksinadd",
      component: () => import('@/views/inventory/stocksin/add'),
      name: 'stocksinadd',
      meta: {
        title: 'New Stock In',
        icon: 'user'
      }
    }]
  },
  {
    path: '/stocksin',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editstocksin",
      component: () => import('@/views/inventory/stocksin/add'),
      name: 'editstocksin',
      meta: {
        title: 'Edit Stock In',
        icon: 'user'
      }
    }]
  },
  {
    path: '/product',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "productadd",
      component: () => import('@/views/product/product/add'),
      name: 'productadd',
      meta: {
        title: 'New product',
        icon: 'user'
      }
    }]
  },
  {
    path: '/product',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editproduct",
      component: () => import('@/views/product/product/add'),
      name: 'editproduct',
      meta: {
        title: 'Edit Product',
        icon: 'user'
      }
    }]
  },
  {
    path: '/supplier',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "supplieradd",
      component: () => import('@/views/supplier/supplier/add'),
      name: 'supplieradd',
      meta: {
        title: 'New Supplier',
        icon: 'user'
      }
    }]
  },
  {
    path: '/supplier',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editsupplier",
      component: () => import('@/views/supplier/supplier/add'),
      name: 'editsupplier',
      meta: {
        title: 'Edit supplier',
        icon: 'user'
      }
    }]
  },
  {
    path: '/customer',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "customeradd",
      component: () => import('@/views/customer/customer/add'),
      name: 'customeradd',
      meta: {
        title: 'New Customer',
        icon: 'user'
      }
    }]
  },
  {
    path: '/customer',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editcustomer",
      component: () => import('@/views/customer/customer/add'),
      name: 'editcustomer',
      meta: {
        title: 'Edit Customer',
        icon: 'user'
      }
    }]
  },
  {
    path: '/customergroup',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "groupadd",
      component: () => import('@/views/customer/customer_group/add'),
      name: 'groupadd',
      meta: {
        title: 'New Group',
        icon: 'user'
      }
    }]
  },
  {
    path: '/customergroup',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editgroup",
      component: () => import('@/views/customer/customer_group/add'),
      name: 'editgroup',
      meta: {
        title: 'Edit Group',
        icon: 'user'
      }
    }]
  },
  {
    path: '/saleorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "saleorderadd",
      component: () => import('@/views/salemodel/saleorder/add'),
      name: 'saleorderadd',
      meta: {
        title: 'New Saleorder',
        icon: 'user'
      }
    }]
  },
  {
    path: '/saleorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editsaleorder",
      component: () => import('@/views/salemodel/saleorder/add'),
      name: 'editsaleorder',
      meta: {
        title: 'Edit Saleorder',
        icon: 'user'
      }
    }]
  },

  {
    path: '/customerpayback',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "customerpaybackadd",
      component: () => import('@/views/report/customerpaybackreport/add'),
      name: 'customerpaybackadd',
      meta: {
        title: 'New Customerpayment',
        icon: 'user'
      }
    }]
  },
  {
    path: '/customerpayback',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editcustomerpayback",
      component: () => import('@/views/report/customerpaybackreport/add'),
      name: 'editcustomerpayback',
      meta: {
        title: 'Edit Customerpayment',
        icon: 'user'
      }
    }]
  },



  {
    path: '/quotationlist',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "quotationlistadd",
      component: () => import('@/views/salemodel/quotationlist/add'),
      name: 'quotationlistadd',
      meta: {
        title: 'New Quotation',
        icon: 'user'
      }
    }]
  },
  {
    path: '/quotationlist',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editquotationlist",
      component: () => import('@/views/salemodel/quotationlist/add'),
      name: 'editquotationlist',
      meta: {
        title: 'Edit Quotation',
        icon: 'user'
      }
    }]
  },
  {
    path: '/saleinvoice',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "saleinvoiceadd",
      component: () => import('@/views/salemodel/saleinvoice/add'),
      name: 'saleinvoiceadd',
      meta: {
        title: 'New Sale Invoice',
        icon: 'user'
      }
    }]
  },
  {
    path: '/saleinvoice',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editsaleinvoice",
      component: () => import('@/views/salemodel/saleinvoice/add'),
      name: 'editsaleinvoice',
      meta: {
        title: 'Edit Sale Invoice',
        icon: 'user'
      }
    }]
  },
  {
    path: '/purchaseorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "purchaseorderadd",
      component: () => import('@/views/puchase/purchaseorder/add'),
      name: 'purchaseorderadd',
      meta: {
        title: 'New Purchase Order',
        icon: 'user'
      }
    }]
  },
  {
    path: '/purchaseorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editpurchaseorder",
      component: () => import('@/views/puchase/purchaseorder/add'),
      name: 'editpurchaseorder',
      meta: {
        title: 'Edit Purchase Order',
        icon: 'user'
      }
    }]
  },
  {
    path: '/purchasereturnlist',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "purchasereturnlistadd",
      component: () => import('@/views/puchase/purchasereturnlist/add'),
      name: 'purchasereturnlistadd',
      meta: {
        title: 'New Purchase Return',
        icon: 'user'
      }
    }]
  },
  {
    path: '/purchasereturnlist',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editpurchasereturnlist",
      component: () => import('@/views/puchase/purchasereturnlist/add'),
      name: 'editpurchasereturnlist',
      meta: {
        title: 'Edit Purchase Return',
        icon: 'user'
      }
    }]
  },
  {
    path: '/quotationtemplate',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "quotationtemplateadd",
      component: () => import('@/views/quotationtemplate/quotationtemplatelist/add'),
      name: 'quotationtemplateadd',
      meta: {
        title: 'New Quotation Template',
        icon: 'user'
      }
    }]
  },
  {
    path: '/quotationtemplate',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editquotationtemplate",
      component: () => import('@/views/quotationtemplate/quotationtemplatelist/add'),
      name: 'editquotationtemplate',
      meta: {
        title: 'Edit Quotation Template',
        icon: 'user'
      }
    }]
  },
  {
    path: '/transferorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "transferorderadd",
      component: () => import('@/views/inventory/transferorder/add'),
      name: 'transferorderadd',
      meta: {
        title: 'New Transferorder',
        icon: 'user'
      }
    }]
  },
  {
    path: '/transferorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "edittransferorder",
      component: () => import('@/views/inventory/transferorder/add'),
      name: 'edittransferorder',
      meta: {
        title: 'Edit Transferorder',
        icon: 'user'
      }
    }]
  },
  {
    path: '/uomorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "assemblyadd",
      component: () => import('@/views/inventory/uomorder/add'),
      name: 'uomorderassemblyadd',
      meta: {
        title: 'New Assembly UOM',
        icon: 'user'
      }
    }]
  },
  {
    path: '/uomorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "disassemblyadd",
      component: () => import('@/views/inventory/uomorder/add2'),
      name: 'uomorderdisassemblyadd',
      meta: {
        title: 'New Disassembly UOM',
        icon: 'user'
      }
    }]
  },
  {
    path: '/uomorder',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "edituomorder",
      component: () => import('@/views/inventory/uomorder/edit'),
      name: 'edituomorder',
      meta: {
        title: 'Edit UOM Order',
        icon: 'user'
      }
    }]
  },
  {
    path: '/stocktake',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "stocktakeadd",
      component: () => import('@/views/inventory/stocktake/add'),
      name: 'stocktakeadd',
      meta: {
        title: 'New Stock Take',
        icon: 'user'
      }
    }]
  },
  {
    path: '/stocktake',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editstocktake",
      component: () => import('@/views/inventory/stocktake/add'),
      name: 'editstocktake',
      meta: {
        title: 'Edit Stock Take',
        icon: 'user'
      }
    }]
  },
  {
    path: '/warehouse',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "warehouseadd",
      component: () => import('@/views/inventory/warehouse/add'),
      name: 'warehouseadd',
      meta: {
        title: 'New Warehouse',
        icon: 'user'
      }
    }]
  },
  {
    path: '/warehouse',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [{
      path: "editwarehouse",
      component: () => import('@/views/inventory/warehouse/add'),
      name: 'editwarehouse',
      meta: {
        title: 'Edit Warehouse',
        icon: 'user'
      }
    }]
  },
  // 404 page must be placed at the end !!!
  {
    path: "*",
    redirect: "/404",
    hidden: true
  }
];

const createRouter = () =>
  new Router({
    mode: "history", // require service support
    base: "/",
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
